package com.opentd.graphics;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.net.URL;

import javax.swing.ImageIcon;

import com.opentd.game.Game;

public class Button extends Rectangle implements Drawable{
	
	int x, y, width, height, type;
	Image background, background_hover;
	
	/**
	 * Creates a new button with the specified type. Will use the
	 * background image and size of the specified type if available.
	 * Otherwise, the background will be grey with a black outline,
	 * and there will be no image.
	 * @param x
	 * @param y
	 * @param type
	 */
	public Button(int x, int y, int type){
		this.x = x;
		this.y = y;
		this.type = type;
		
		switch(type){
		case Game.Button.START_BUTTON:
			background = new ImageIcon("dat/img/button_start.png").getImage();
			background_hover = new ImageIcon("dat/img/button_start_hover.png").getImage();
			width = 200;
			height = 50;
			break;
		case Game.Button.LEVEL_SELECT_BUTTON:
			background = new ImageIcon("dat/img/button_level_select.png").getImage();
			background_hover = new ImageIcon("dat/img/button_level_select_hover.png").getImage();
			width = 200;
			height = 50;
			break;
		case Game.Button.PAUSE_BUTTON:
			background = new ImageIcon("dat/img/button_pause.png").getImage();
			width = 30;
			height = 30;
			break;
		case Game.Button.QUIT_BUTTON:
			background = new ImageIcon("dat/img/button_quit.png").getImage();
			background_hover = new ImageIcon("dat/img/button_quit_hover.png").getImage();
			width = 200;
			height = 50;
			break;
		case Game.Button.RESTART_BUTTON:
			background = new ImageIcon("dat/img/button_restart.png").getImage();
			background_hover = new ImageIcon("dat/img/button_restart_hover.png").getImage();
			width = 200;
			height = 50;
			break;
		case Game.Button.BACK_BUTTON:
			background = new ImageIcon("dat/img/button_back.png").getImage();
			background_hover = new ImageIcon("dat/img/button_back_hover.png").getImage();
			width = 200;
			height = 50;
			break;
		case Game.Button.TOWER_BUTTON:
			background = new ImageIcon("dat/img/button_tower.png").getImage();
			background_hover = new ImageIcon("dat/img/button_tower_hover.png").getImage();
			width = 50;
			height = 50;
			break;
		default:
			width = 50;
			height = 50;
			background = null;
		}
	}
	
	/**
	 * Creates a new button with all of the specified parameters.
	 * <p>
	 * Will throw an exception if the specified file is not found.
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @param type
	 * @param bgImgPath
	 */
	public Button(int x, int y, int width, int height, int type, URL bgImgPath){
		this.x = x;
		this.y = y;
		this.type = type;
		this.width = width;
		this.height = height;
		background = new ImageIcon(bgImgPath).getImage();
		
	}
	
	@Override
	public void draw(Graphics g) {
		if(isHovered()){
			g.drawImage(background_hover, x, y, width, height, null);
			//System.out.println("Hovering!");
		}else{
			g.drawImage(background, x, y, width, height, null);
		}
		//g.drawString("Bounds: x = " + this.x + ", y = " + this.y, x, y);
	}
	
	public void setBackground(Image img){
		background = img;
	}
	
	public boolean contains(Point p){
		return new Rectangle(x, y + 25, width, height).contains(p);
	}
	
	public boolean isHovered(){
		return new Rectangle(x, y + 25, width, height).contains(Game.mousePos);
	}

}
